{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<i>Copyright (c) Microsoft Corporation. All rights reserved.</i>\n",
    "\n",
    "<i>Licensed under the MIT License.</i>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# DKN : Deep Knowledge-Aware Network for News Recommendation\n",
    "DKN \\[1\\] is a deep learning model which incorporates information from knowledge graph for better news recommendation. Specifically, DKN uses TransX \\[2\\] method for knowledge graph representaion learning, then applies a CNN framework, named KCNN, to combine entity embedding with word embedding and generate a final embedding vector for a news article. CTR prediction is made via an attention-based neural scorer. \n",
    "\n",
    "## Properties of DKN:\n",
    "- DKN is a content-based deep model for CTR prediction rather than traditional ID-based collaborative filtering. \n",
    "- It makes use of knowledge entities and common sense in news content via joint learning from semantic-level and knnowledge-level representations of news articles.\n",
    "- DKN uses an attention module to dynamically calculate a user's aggregated historical representaition.\n",
    "\n",
    "## Reference environment \n",
    "we tested this notebook with two environment settings:\n",
    "cpu with tensorflow==1.15.2 and gpu with tensorflow==1.15.2 on July 1, 2020.\n",
    "\n",
    "## Data format:\n",
    "### DKN takes several files as input as follows:\n",
    "- training / validation / test files: each line in these files represents one instance. Impressionid is used to evaluate performance within an impression session, so it is only used when evaluating, you can set it to 0 for training data. The format is : <br> \n",
    "`[label] [userid] [CandidateNews]%[impressionid] `<br> \n",
    "e.g., `1 train_U1 N1%0` <br> \n",
    "- user history file: each line in this file represents a users' click history. You need to set history_size parameter in config file, which is the max number of user's click history we use. We will automatically keep the last history_size number of user click history, if user's click history is more than history_size, and we will automatically padding 0 if user's click history less than history_size. the format is : <br> \n",
    "`[Userid] [newsid1,newsid2...]`<br>\n",
    "e.g., `train_U1 N1,N2` <br> \n",
    "- document feature file:\n",
    "It contains the word and entity features of news. News article is represented by (aligned) title words and title entities. To take a quick example, a news title may be : Trump to deliver State of the Union address next week , then the title words value may be CandidateNews:34,45,334,23,12,987,3456,111,456,432 and the title entitie value may be: entity:45,0,0,0,0,0,0,0,0,0. Only the first value of entity vector is non-zero due to the word Trump. The title value and entity value is hashed from 1 to n(n is the number of distinct words or entities). Each feature length should be fixed at k(doc_size papameter), if the number of words in document is more than k, you should truncate the document to k words, and if the number of words in document is less than k, you should padding 0 to the end. \n",
    "the format is like: <br> \n",
    "`[Newsid] [w1,w2,w3...wk] [e1,e2,e3...ek]`\n",
    "- word embedding/entity embedding/ context embedding files: These are npy files of pretrained embeddings. After loading, each file is a [n+1,k] two-dimensional matrix, n is the number of words(or entities) of their hash dictionary, k is dimension of the embedding, note that we keep embedding 0 for zero padding. \n",
    "In this experiment, we used GloVe\\[4\\] vectors to initialize the word embedding. We trained entity embedding using TransE\\[2\\] on knowledge graph and context embedding is the average of the entity's neighbors in the knowledge graph.<br>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Global settings and imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append(\"../../\")\n",
    "from reco_utils.recommender.deeprec.deeprec_utils import *\n",
    "from reco_utils.recommender.deeprec.models.dkn import *\n",
    "from reco_utils.recommender.deeprec.io.dkn_iterator import *\n",
    "import papermill as pm\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download and load data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "data_path = '../../tests/resources/deeprec/dkn'\n",
    "yaml_file = os.path.join(data_path, r'dkn.yaml')\n",
    "train_file = os.path.join(data_path, r'train_mind_demo.txt')\n",
    "valid_file = os.path.join(data_path, r'valid_mind_demo.txt')\n",
    "test_file = os.path.join(data_path, r'test_mind_demo.txt')\n",
    "news_feature_file = os.path.join(data_path, r'doc_feature.txt')\n",
    "user_history_file = os.path.join(data_path, r'user_history.txt')\n",
    "wordEmb_file = os.path.join(data_path, r'word_embeddings_100.npy')\n",
    "entityEmb_file = os.path.join(data_path, r'TransE_entity2vec_100.npy')\n",
    "contextEmb_file = os.path.join(data_path, r'TransE_context2vec_100.npy')\n",
    "infer_embedding_file = os.path.join(data_path, r'infer_embedding.txt')\n",
    "if not os.path.exists(yaml_file):\n",
    "    download_deeprec_resources(r'https://recodatasets.blob.core.windows.net/deeprec/', data_path, 'mind-demo.zip')\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Create hyper-parameters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false
    },
    "tags": [
     "parameters"
    ]
   },
   "outputs": [],
   "source": [
    "epoch=5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[('DNN_FIELD_NUM', None), ('EARLY_STOP', 100), ('FEATURE_COUNT', None), ('FIELD_COUNT', None), ('L', None), ('MODEL_DIR', None), ('PAIR_NUM', None), ('SUMMARIES_DIR', None), ('T', None), ('activation', ['sigmoid']), ('att_fcn_layer_sizes', None), ('attention_activation', 'relu'), ('attention_dropout', 0.0), ('attention_layer_sizes', 100), ('attention_size', None), ('batch_size', 100), ('cate_embedding_dim', None), ('cate_vocab', None), ('contextEmb_file', '../../tests/resources/deeprec/dkn\\\\TransE_context2vec_100.npy'), ('cross_activation', 'identity'), ('cross_l1', 0.0), ('cross_l2', 0.0), ('cross_layer_sizes', None), ('cross_layers', None), ('data_format', 'dkn'), ('dim', 100), ('doc_size', 10), ('dropout', [0.0]), ('dtype', 32), ('embed_l1', 0.0), ('embed_l2', 1e-06), ('embedding_dropout', 0.3), ('enable_BN', False), ('entityEmb_file', '../../tests/resources/deeprec/dkn\\\\TransE_entity2vec_100.npy'), ('entity_dim', 100), ('entity_embedding_method', 'TransE'), ('entity_size', 1406), ('epochs', 5), ('fast_CIN_d', 0), ('filter_sizes', [1, 2, 3]), ('hidden_size', None), ('init_method', 'uniform'), ('init_value', 0.1), ('is_clip_norm', 0), ('item_embedding_dim', None), ('item_vocab', None), ('iterator_type', None), ('kg_file', None), ('kg_training_interval', 5), ('layer_l1', 0.0), ('layer_l2', 1e-06), ('layer_sizes', [300]), ('learning_rate', 5e-05), ('load_model_name', None), ('load_saved_model', False), ('loss', 'log_loss'), ('lr_kg', 0.5), ('lr_rs', 1), ('max_grad_norm', 2), ('max_seq_length', None), ('method', 'classification'), ('min_seq_length', 1), ('model_type', 'dkn'), ('mu', None), ('n_h', None), ('n_item', None), ('n_item_attr', None), ('n_user', None), ('n_user_attr', None), ('n_v', None), ('need_sample', True), ('news_feature_file', '../../tests/resources/deeprec/dkn\\\\doc_feature.txt'), ('num_filters', 100), ('optimizer', 'adam'), ('pairwise_metrics', ['group_auc', 'mean_mrr', 'ndcg@5;10']), ('reg_kg', 0.0), ('save_epoch', 2), ('save_model', False), ('show_step', 1000), ('train_num_ngs', 4), ('train_ratio', None), ('transform', True), ('use_CIN_part', False), ('use_DNN_part', False), ('use_FM_part', False), ('use_Linear_part', False), ('use_context', True), ('user_clicks', None), ('user_dropout', False), ('user_embedding_dim', None), ('user_history_file', '../../tests/resources/deeprec/dkn\\\\user_history.txt'), ('user_vocab', None), ('wordEmb_file', '../../tests/resources/deeprec/dkn\\\\word_embeddings_100.npy'), ('word_size', 5947), ('write_tfevents', False)]\n"
     ]
    }
   ],
   "source": [
    "hparams = prepare_hparams(yaml_file,\n",
    "                          news_feature_file = news_feature_file,\n",
    "                          user_history_file = user_history_file,\n",
    "                          wordEmb_file=wordEmb_file,\n",
    "                          entityEmb_file=entityEmb_file,\n",
    "                          contextEmb_file=contextEmb_file,\n",
    "                          epochs=epoch)\n",
    "print(hparams)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "input_creator = DKNTextIterator"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train the DKN model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "model = DKN(hparams, input_creator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'group_auc': 0.5, 'mean_mrr': 0.0759, 'ndcg@5': 0.0567, 'ndcg@10': 0.0783}\n"
     ]
    }
   ],
   "source": [
    "print(model.run_eval(valid_file))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "at epoch 1\n",
      "train info: logloss loss:0.6025281803948539\n",
      "eval info: group_auc:0.5726, mean_mrr:0.2035, ndcg@10:0.2689, ndcg@5:0.203\n",
      "at epoch 1 , train time: 537.5 eval time: 1068.0\n",
      "at epoch 2\n",
      "train info: logloss loss:0.5871794990130833\n",
      "eval info: group_auc:0.5789, mean_mrr:0.2123, ndcg@10:0.2756, ndcg@5:0.2076\n",
      "at epoch 2 , train time: 473.6 eval time: 984.5\n",
      "at epoch 3\n",
      "train info: logloss loss:0.5733468277113778\n",
      "eval info: group_auc:0.584, mean_mrr:0.2182, ndcg@10:0.2847, ndcg@5:0.2121\n",
      "at epoch 3 , train time: 518.1 eval time: 830.4\n",
      "at epoch 4\n",
      "train info: logloss loss:0.5610611098153251\n",
      "eval info: group_auc:0.5956, mean_mrr:0.2231, ndcg@10:0.2869, ndcg@5:0.2158\n",
      "at epoch 4 , train time: 456.8 eval time: 1002.5\n",
      "at epoch 5\n",
      "train info: logloss loss:0.5502915297235761\n",
      "eval info: group_auc:0.601, mean_mrr:0.2257, ndcg@10:0.2882, ndcg@5:0.2177\n",
      "at epoch 5 , train time: 398.1 eval time: 956.4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<reco_utils.recommender.deeprec.models.dkn.DKN at 0x9d5b94d278>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train_file, valid_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can test again the performance on valid set:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'group_auc': 0.601, 'mean_mrr': 0.2257, 'ndcg@5': 0.2177, 'ndcg@10': 0.2882}\n"
     ]
    }
   ],
   "source": [
    "res = model.run_eval(valid_file)\n",
    "print(res)\n",
    "pm.record(\"res\", res)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Document embedding inference API\n",
    "After training, you can get document embedding through this document embedding inference API. The input file format is same with document feature file. The output file fomrat is: `[Newsid] [embedding]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "pycharm": {
     "is_executing": false,
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "model.run_get_embedding(news_feature_file, infer_embedding_file)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Quick start with MINDsmall dataset\n",
    "To help you get a quick start of using DKN on MIND dataset, we offer a script to transform MINDsmall dataset to Microsoft Recommender DKN data format:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "building MINDsmall train, valid, user_history\n",
      "finished building MINDsmall train, valid, user_history\n",
      "building MINDsmall doc_features\n",
      "finished building MINDsmall doc_features\n"
     ]
    }
   ],
   "source": [
    "! bash ../../tests/resources/deeprec/dkn/MINDsmall/scripts/build_dkn_data.sh"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "Now we start to running on MINDsmall dataset:"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [],
   "source": [
    "data_path = '../../tests/resources/deeprec/dkn/MINDsmall/'\n",
    "yaml_file = os.path.join(data_path, r'dkn_MINDsmall.yaml')\n",
    "train_file = os.path.join(data_path, r'train_mind_small.txt')\n",
    "valid_file = os.path.join(data_path, r'valid_mind_small.txt')\n",
    "news_feature_file = os.path.join(data_path, r'doc_feature.txt')\n",
    "user_history_file = os.path.join(data_path, r'user_history_small.txt')\n",
    "wordEmb_file = os.path.join(data_path, r'word_embeddings_5w_100.npy')\n",
    "entityEmb_file = os.path.join(data_path, r'entity_embeddings_5w_100.npy')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [],
   "source": [
    "hparams = prepare_hparams(yaml_file,\n",
    "                          news_feature_file = news_feature_file,\n",
    "                          user_history_file = user_history_file,\n",
    "                          wordEmb_file=wordEmb_file,\n",
    "                          entityEmb_file=entityEmb_file,\n",
    "                          epochs=5)\n",
    "input_creator = DKNTextIterator\n",
    "model = DKN(hparams, input_creator)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 10000 , total_loss: 0.4546, data_loss: 0.4543\n",
      "at epoch 1\n",
      "train info: logloss loss:0.47117363242787097\n",
      "eval info: group_auc:0.5962, mean_mrr:0.2717, ndcg@10:0.3559, ndcg@5:0.2962\n",
      "at epoch 1 , train time: 570.7 eval time: 427.9\n",
      "step 10000 , total_loss: 0.4566, data_loss: 0.4557\n",
      "at epoch 2\n",
      "train info: logloss loss:0.4610959834723853\n",
      "eval info:, group_auc:0.6156, mean_mrr:0.2816, ndcg@10:0.3679, ndcg@5:0.3056\n",
      "at epoch 2 , train time: 567.1 eval time: 431.2\n",
      "step 10000 , total_loss: 0.4575, data_loss: 0.4562\n",
      "at epoch 3\n",
      "train info: logloss loss:0.4534366399952554\n",
      "eval info: group_auc:0.6161, mean_mrr:0.2825, ndcg@10:0.3682, ndcg@5:0.3064\n",
      "at epoch 3 , train time: 567.8 eval time: 450.8\n",
      "step 10000 , total_loss: 0.4641, data_loss: 0.4625\n",
      "at epoch 4\n",
      "train info: logloss loss:0.4478095957073469\n",
      "eval info: group_auc:0.6164, mean_mrr:0.2828, ndcg@10:0.3687, ndcg@5:0.3067\n",
      "at epoch 4 , train time: 568.5 eval time: 449.0\n",
      "step 10000 , total_loss: 0.4564, data_loss: 0.4545\n",
      "at epoch 5\n",
      "train info: logloss loss:0.44312271486131205\n",
      "eval info: group_auc:0.6162, mean_mrr:0.2826, ndcg@10:0.3686, ndcg@5:0.3061\n",
      "at epoch 5 , train time: 567.4 eval time: 447.9\n"
     ]
    }
   ],
   "source": [
    "model.fit(train_file, valid_file)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Running models with large dataset\n",
    "Here are performances using the whole MIND dataset \\[3\\]. \n",
    "\n",
    "MIND dataset is a large-scale English news dataset. It was collected from anonymized behavior logs of Microsoft News website. MIND contains 1,000,000 users, 161,013 news articles and 15,777,377 impression logs. Every news article contains rich textual content including title, abstract, body, category and entities. Each impression log contains the click events, non-clicked events and historical news click behaviors of this user before this impression.\n",
    "\n",
    "| Models | g-AUC | MRR |NDCG@5 | NDCG@10 |\n",
    "| :------| :------: | :------: | :------: | :------ |\n",
    "| LibFM | 0.5993 | 0.2823 | 0.3005 | 0.3574 |\n",
    "| Wide&Deep | 0.6216 | 0.2931 | 0.3138 | 0.3712 |\n",
    "| DKN | 0.6436 | 0.3128 | 0.3371 | 0.3908|\n",
    "\n",
    "\n",
    "Note that the results of DKN are using Microsoft recommender and the results of the first two models come from the MIND paper \\[3\\].\n",
    "We compare the results on the same test dataset. \n",
    "\n",
    "One epoch takes 6381.3s (5066.6s for training, 1314.7s for evaluating) for DKN on GPU. Hardware specification for running the large dataset: <br>\n",
    "GPU: Tesla P100-PCIE-16GB <br>\n",
    "CPU: 6 cores Intel(R) Xeon(R) CPU E5-2690 v4 @ 2.60GHz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reference\n",
    "\\[1\\] Wang, Hongwei, et al. \"DKN: Deep Knowledge-Aware Network for News Recommendation.\" Proceedings of the 2018 World Wide Web Conference on World Wide Web. International World Wide Web Conferences Steering Committee, 2018.<br>\n",
    "\\[2\\] Knowledge Graph Embeddings including TransE, TransH, TransR and PTransE. https://github.com/thunlp/KB2E <br>\n",
    "\\[3\\] Wu, Fangzhao, et al. \"MIND: A Large-scale Dataset for News Recommendation\" Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics. https://msnews.github.io/competition.html <br>\n",
    "\\[4\\] GloVe: Global Vectors for Word Representation. https://nlp.stanford.edu/projects/glove/"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Tags",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.10"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
